<template>
  <div style="height: 100%; display: flex; flex-direction: column;">
    <BreadFadeWidget ref="refBread" :start-index="2" style="padding: 8px 4px 0 8px"
                     :root-path="config.rootItem" :detail-label="config.detail" :trigger-path="triggerPath"/>
    <TableHeaderWidget :header="current().name" />
    <RealmUserAssignList ref="refList" v-show="config.isRoot"
                         :add-group="addUserGroupToPlatform"
                         :remove-group="removeUserGroupFromPlatform"
                         :load-user-group="loadCanAssignToPlatformUserGroup"
                         :load-assign-group="loadAssignedToPlatformUserGroup"/>
    <PlatformRealmDetail v-show="!config.isRoot" ref="refDetail"/>
  </div>
</template>

<script setup lang="ts" name="platformRealm">
import {useListPage} from "@/utils/UseListPage";
import {currentNames} from "@/stores/conf";
import TableHeaderWidget from "@/widget/TableHeaderWidget.vue";
import BreadFadeWidget from "@/widget/BreadFadeWidget.vue";
import PlatformRealmDetail from "@/views/pages/platform/PlatformRealmDetail.vue";
import RealmUserAssignList from "@/widget/list/RealmUserAssignList.vue";
import {
  loadRealmUserGroupOfPlatformList,
  loadRealmUserGroupLevelPage,
  assignRealmUserGroupsToPlatform,
  removeRealmUserGroupFromPlatform
} from "@/apis";
import {useStore} from "@/stores";

const config = {
  isRoot: true,
  detail: '分组详情',
  rootItem: {id: '', name: '平台领域信息',}
}

const store = useStore();
const {refBread, refList, refDetail, current, triggerPath} = useListPage(currentNames.PLAT_REALM_USER, config);

/**
 * 把选定的领域用户分组分配给当前平台（这样分组内用户就可以访问指定平台）
 * @param items
 */
const addUserGroupToPlatform = async (items: any) => {
  const platform = store.getCurrentPlatform();
  /*
   * 把选择的领域用户分组分配给当前平台（分组内用户平台就可以访问）
   */
  return await assignRealmUserGroupsToPlatform({
    realmId: platform.realmId,
    platformId: platform.id,
    groups: items,
  });
}

const removeUserGroupFromPlatform = async (group: any) => {
  const groups = [];
  const platform = store.getCurrentPlatform();

  groups.push(group.id);

  return await removeRealmUserGroupFromPlatform({
    realmId: platform.realmId,
    platformId: platform.id,
    groups: groups,
  });
}

/**
 * 读取能够分配给当前平台的领域用户分组
 * 此方法传给LevelSelectorDialog的load方法，其中parentId由LevelSelectorDialog填入
 * @param params
 */
const loadCanAssignToPlatformUserGroup = async (params: any) => {
  const platform = store.getCurrentPlatform();

  /*
   * 按层分页读取领域用户的分组信息
   * 这里传入平台ID，过滤掉已经分配给了平台的领域用户分组
   */
  const result = await loadRealmUserGroupLevelPage({
    realmId: platform.realmId,
    filter: {
      platformId: platform.id,
    },
    ...params
  });

  return result;
}

/**
 * 加载分配给当前平台的领域用户分组
 * @param params
 */
const loadAssignedToPlatformUserGroup = async (params: any) => {
  const platform = store.getCurrentPlatform();
  /*
   * 读取分配给当前平台的领域用户分组（可以把领域的一部分用户分配给平台）
   */
  const result = await loadRealmUserGroupOfPlatformList({
    platformId: platform.id,
    realmId: platform.realmId,
    ...params
  });

  if (result && result.code == 0 && result.data) {
    result.data = {
      count: result.data.length,
      current: 1,
      pageSize: result.data.length,
      total: result.data.length,
      data: result.data
    }
  }

  return result;
}

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}
</style>
